<?php

namespace App\Http\Controllers\API;

use App\Model\SecKillGoods;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redis;

class SecKillGoodsController extends Controller
{
    /**
     * @abstract get all seckillgoods data
     * @return \Illuminate\Database\Eloquent\Collection|static[]
     */
    public function index(Request $request){
        $arrRequestParams = $request->all();
        if(array_key_exists('title',$arrRequestParams) && $arrRequestParams['title'] != ''){
            $seckillgoods = DB::table('seckillgoods')->where('title', 'like', '%'.$arrRequestParams['title'].'%')->get();
        }else{
            $seckillgoods = DB::table('seckillgoods')->get();
        }
        return response()->json(['code' => 0, 'message' => 'success', 'data' => $seckillgoods],200);
    }

    /**
     * @abstract get seckillgoods by id
     * @param $id
     * @return mixed
     */
    public function show($id){
        $seckillgood =  SecKillGoods::find($id);
        return $seckillgood;
    }

    /**
     * @abstract add new seckillgoods
     * @param Request $request
     * @return mixed
     */
    public function store(Request $request){
        $arrPostData = $request->all();
        $created_at = date('Y-m-d H:i:s',time());
        $seckillgoods = new SecKillGoods();
        $seckillgoods->title = $arrPostData['title'];
        $seckillgoods->price = $arrPostData['price'];
        $seckillgoods->path = $arrPostData['path'];
        $seckillgoods->number_left = $arrPostData['number_left'];
        $seckillgoods->creator = 1;
        $seckillgoods->created_at = $created_at;
        $seckillgoods->status = 0;
        $seckillgoods->lasttime = $arrPostData['lasttime'];
        $seckillgoods->save();
        return response()->json(['code' => 0,'message' => 'save success'],200);
    }

    /**
     * @abstract update seckillgoods by id
     * @param Request $request
     * @param $id
     * @return mixed
     */
    public function update(Request $request,$id){
        $arrPostData = $request->all();
        $objProduct = SecKillGoods::findOrFail($id);
        $objProduct->title = $arrPostData['title'];
        $objProduct->price = $arrPostData['price'];
        $objProduct->path = $arrPostData['path'];
        $objProduct->number_left = $arrPostData['number_left'];
        $objProduct->lasttime = $arrPostData['lasttime'];
        $objProduct->save();
        return response()->json(['code' => 0,'message' => 'update success'],200);
    }

    /**
     * @abstract delete seckillgoods by id
     * @param $id
     * @return mixed
     */
    public function delete($id){
        $seckillgood = SecKillGoods::findOrFail($id);
        $seckillgood->delete();
        return response()->json(['code' => 0,'message' => 'delete success'],200);
    }

    /**
     * @abstract publish seckillgoods
     * @param $id
     * @return \Illuminate\Http\JsonResponse
     */
    public function publishSeckillGoods($id){
        $flag = SecKillGoods::addSecKillGoodsToRedisCache($id);
        if($flag){
            return response()->json(['code' => 0,'message' => 'publish success'],200);
        }else{
            return response()->json(['code' => 1,'message' => 'publish failed'],200);
        }
    }


    /**
     * @abstract weuser buy seckillgoods
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function buySecKillGoods(Request $request){
        $arrData = $request->all();
        $objSecKillGoods = new SecKillGoods();
        $result = $objSecKillGoods->buySecKillGoodsById($arrData);
        if($result === false){//下单失败
            return response()->json(['code' => 1,'message' => 'no seckillgoods store'],200);
        }else{
            return response()->json(['code' => 0,'message' => 'buy seckillgoods success', 'order_no' => $result],200);
        }
    }
}
